package com.ghsc.ghxc.jsb.ui.integral.activity

import android.os.Bundle
import android.text.TextUtils
import com.cn.appcore.base.BaseVMActivity
import com.cn.appcore.ext.asColor
import com.cn.appcore.ext.dp
import com.cn.appcore.ext.setSingleClickListener
import com.cn.appcore.ext.showToast
import com.cn.appcore.ext.startKtxActivity
import com.cn.appcore.utils.GlideUtil
import com.cn.appcore.utils.GsonUtil
import com.ghsc.ghxc.jsb.R
import com.ghsc.ghxc.jsb.config.CustKey
import com.ghsc.ghxc.jsb.config.LoginHelper
import com.ghsc.ghxc.jsb.databinding.ActivitySubIntegralOrderBinding
import com.ghsc.ghxc.jsb.ext.setGone
import com.ghsc.ghxc.jsb.ext.setOnFastClickListener
import com.ghsc.ghxc.jsb.ext.setVisible
import com.ghsc.ghxc.jsb.ui.integral.IntegralSupermarketViewModel
import com.ghsc.ghxc.jsb.ui.integral.bean.IntegralSkuBean
import com.ghsc.ghxc.jsb.ui.my.activity.AddressActivity
import com.ghsc.ghxc.jsb.ui.my.bean.AddressDetailBean

/**
 * 项目：GuoHuaJsb
 * 包名：com.ghsc.ghxc.jsb.ui.integral.activity
 * 类名：IntegralSubOrderActivity
 * 创建时间：2024/5/22 15:35
 * 创建人 @author： xiaolin
 * 描述 ：Todo...积分 订单提交
 */
class IntegralSubOrderActivity : BaseVMActivity<IntegralSupermarketViewModel, ActivitySubIntegralOrderBinding>()  {
    override fun attachLayoutRes(): Int {
        return  R.layout.activity_sub_integral_order
    }

    override fun attachVMClass(): Class<IntegralSupermarketViewModel>? {
        return IntegralSupermarketViewModel::class.java
    }

    var bundle: Bundle?=null
    var skuItemBean : IntegralSkuBean?=null
    var itemCnt=0

    override fun initImmersionBar() {
        super.initImmersionBar()
        mImmersionBar!!.statusBarDarkFont(true).init()
    }



    private var shouhuoid:String = ""
    private var addressDetailBean: AddressDetailBean? = null


    override fun initView() {
        bundle=intent.extras
        if (bundle!=null){
            skuItemBean= bundle!!.getSerializable("skuItemBean") as IntegralSkuBean?
            itemCnt=bundle!!.getInt("itemCnt",0)
        }

        binding.bar.topTitle.text="提交订单"
        binding.bar.topLeftView.setSingleClickListener { finish() }


        skuItemBean?.let {
            GlideUtil.ImageLoad(this,it.images,binding.ivCommodityImg)
            binding.tvCommodityName.text=it.skuname
            binding.tvPayPrice.text=it.skuprice.toString()
            binding.tvCount.text=itemCnt.toString()
            binding.tvPriceAll.text=it.skuprice.toString()
        }



    }

    override fun onResume() {
        super.onResume()
        mViewModel.onAddressListMethod("")
    }

    override fun initClickListener() {
        super.initClickListener()
        binding.sclAddress.setOnFastClickListener {
            startKtxActivity<AddressActivity>(
                values = arrayListOf(
                    "is_select" to true,
                )
            )
        }
        binding.stvSubmitOrder.setSingleClickListener {
            if (TextUtils.isEmpty(shouhuoid)){
                showToast("请选择收货地址")
                return@setSingleClickListener
            }
            val pids = "${skuItemBean!!.pid}:${skuItemBean!!.skuid}:${itemCnt}"
            val map: HashMap<String, Any> = HashMap()
            map["pids"] = pids //商品id 规格 数量
            map["shouhuoid"] = shouhuoid //收货地址id
            map["oid"] = ""
            GsonUtil.mapToJsonStrObjEncryption(map)?.let { it1 -> mViewModel.onGoodsBuyDataMethod(it1) }
        }
    }



    override fun initRequestObserver() {
        super.initRequestObserver()
        mViewModel.apply {
            errorMsg.observe(this@IntegralSubOrderActivity) {
                showToast(it)
            }
            againLoginMsg.observe(this@IntegralSubOrderActivity) {
                showToast(CustKey.IN_AGAIN_MSG)
                LoginHelper.startLoginPage()
            }
            onAddressListData.observe(this@IntegralSubOrderActivity) {
                it.let {
                    val shapeBuilder = binding.sclAddress.shapeBuilder
                    if (it.isNotEmpty()){
                        //有录入的地址
                        binding.tvEmptyAddress.setGone(true)
                        binding.group.setVisible(true)
                        shapeBuilder?.setShapeSolidColor(R.color.white.asColor())
                        shapeBuilder?.setShapeStrokeColor(R.color.white.asColor())
                        shapeBuilder?.setShapeStrokeWidth(1.dp)
                        it.forEach {
                            if (it.ifdefault == 1){
                                //默认地址
                                binding.tvName.text = "${it.name} ${it.mobile.substring(0,3)}****${it.mobile.substring(7,it.mobile.length)}"
                                binding.tvAddress.text = "${it.province}${if (it.city == it.province) "" else it.city}${it.area}${it.site}"
                                shouhuoid = "${it.id}"
                                addressDetailBean = it
                            }
                        }
                    }else {
                        //无地址
                        binding.group.setGone(true)
                        binding.tvEmptyAddress.setVisible(true)
                        shapeBuilder?.setShapeSolidColor(R.color.color_F9F7EF.asColor())
                        shapeBuilder?.setShapeStrokeColor(R.color.color_F8BD00.asColor())
                        shapeBuilder?.setShapeStrokeWidth(1.dp)
                    }
                    shapeBuilder?.into(binding.sclAddress)
                }
            }
            onGoodsBuyData.observe(this@IntegralSubOrderActivity){
                //
                if (!TextUtils.isEmpty(it.data.data.oid)){
                   startKtxActivity<IntegralOrderDetailActivity>(
                       values = arrayListOf(
                           "oid" to  it.data.data.oid,
                           "type" to 1,
                       )
                    )
                }

            }

        }
    }

}